﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MillerApp.DataContract;
using System.Data.Common;

namespace MillerApp.DataAccess
{
    class DbActions
    {
        public void DD()
        {
            //
        }

        public IDbConnection GetDbConnection()
        {
            Npgsql.NpgsqlConnection conn = new Npgsql.NpgsqlConnection();
            conn.ConnectionString = AppConstants.NpgsqlConnectionString;
            return conn;
        }

        public DataSet ExecuteDataSet(System.Data.CommandType cmdType, string cmdText, System.Data.IDbDataParameter[] commandParameters)
        {
            IDbConnection db;
            DbCommand cmd;
            try
            {
                db = GetDbConnection();
                cmd = PrepareCommand(db, cmdType, cmdText, commandParameters);
                DataSet ds = db.ExecuteDataSet(cmd);
                SetOutPutParameterValue(cmd, commandParameters);
                return ds;
            }
            catch (Exception ex)
            {
                string message = "通用数据访问模块出现异常。\r\n" + "要执行的语句（存储过程）是：" + cmdText + "\r\n" + "具体原因是：" + ex.ToString();
                ElbDatabaseException newEx = new ElbDatabaseException(message, ex);
                ExceptionPolicy.HandleException(newEx, SystemConstants.FrameworkExceptionPolicy);
                throw;
            }
        }

        public string GetProviderName()
        {
            N
            return "";
        }

        /**/
        /// 
        /// 返回数据工厂
        /// 
        /// 
        /// 
        public DbProviderFactory GetDbProviderFactory(string providername)
        {
            //Npgsql.NpgsqlFactory
            return DbProviderFactories.GetFactory(providername);
        }

        public DbProviderFactory GetDbProviderFactory()
        {
            DbProviderFactory f = null;
            string ProviderName = GetProviderName();
            switch (ProviderName)
            {
                case "System.Data.SqlClient":
                    f = GetDbProviderFactory("System.Data.SqlClient");
                    break;
                case "System.Data.OracleClient":
                    f = GetDbProviderFactory("System.Data.OracleClient");
                    break;
                case "System.Data.OleDb":
                    f = GetDbProviderFactory("System.Data.OleDb");
                    break;
                default:
                    f = GetDbProviderFactory("System.Data.SqlClient");
                    break;
            }
            return f;
        }


        private DbCommand CreateCommandByCommandType(CommandType commandType, string commandText)
        {
            System.Data.Common.DbProviderFactory factrory = GetDbProviderFactory();
            DbCommand command = factrory.CreateCommand();
            command.set_CommandType(commandType);
            command.set_CommandText(commandText);
            return command;
        } 

        private DbCommand PrepareCommand(IDbConnection db, CommandType cmdType, string cmdText, IDbDataParameter[] commandParameters)
        {
            DbCommand cmd = null;
            switch (cmdType)
            {
                case CommandType.Text:
                    cmd = db.GetSqlStringCommand(cmdText);
                    break;
                case CommandType.StoredProcedure:
                    cmd = db.GetStoredProcCommand(cmdText);
                    break;
                case CommandType.TableDirect:
                    throw new ElbDatabaseException("通用数据访问模块出现异常：不支持CommandType.TableDirect类型！");

            }
            if (commandParameters == null)
                return cmd;
            if (commandParameters.Length == 0)
                return cmd;
            foreach (IDbDataParameter para in commandParameters)
            {
                IDbDataParameter temppara = cmd.CreateParameter();
                temppara.ParameterName = para.ParameterName;
                temppara.DbType = para.DbType;
                temppara.Direction = para.Direction;
                temppara.SourceColumn = para.SourceColumn;
                temppara.SourceVersion = para.SourceVersion;
                temppara.Value = para.Value;
                temppara.Size = para.Size;
                //cmd.Parameters.Add(para.ParameterName, para.DbType, para.Size, para.Direction, para.IsNullable, para.Precision, para.Scale, para.SourceColumn, para.SourceVersion, para.Value);
                if (para.Value == null)
                    temppara.Value = DBNull.Value;
                cmd.Parameters.Add(temppara);
                //cmd.AddParameter(para.ParameterName, para.DbType, para.Size, para.Direction, para.IsNullable, para.Precision, para.Scale, para.SourceColumn, para.SourceVersion, para.Value);

            }
            return cmd;
        }
    }
}
